/*******************************************************
> Copyright (C) 2024 ==iot== All rights reserved.
> File Name: server.c
> Author: wyf
> Created Time: 2024年08月15日 星期四 11时13分17秒
***************************************************/


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dirent.h> //目录名获取配合DIR
#include <assert.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h> //文件控制操作
#include <sys/types.h> //获取pid等
#include <sys/wait.h> //等待子进程结束
#include <time.h> //时间获取
#include <sys/ipc.h> //进程通信
#include <sys/msg.h> //消息队列
#include <sys/shm.h> //共享内存
#include <signal.h> //信号处理
#include <pthread.h> //线程
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <sys/socket.h>
#include "cJSON.h"
struct msg {
    char operator[10];
    int number1;
    int number2;
    int result;
};

int main(int argc, const char* argv[])
{
    int socketfd = 0;
    int sock = 0;
    int listenfd = 0;
    char buf[1024] = {0};
    int ret = 0;
    struct sockaddr_in addr;
    sock = socket(AF_INET, SOCK_STREAM, 0);
    if (sock == -1) {
        perror("socket");
        exit(-1);
    }

    addr.sin_family = AF_INET;
    addr.sin_port = htons(6891);//-----------------------------------------------------------------------------------------
    addr.sin_addr.s_addr = inet_addr("192.168.48.128");

    ret = bind(sock, (struct sockaddr*)&addr, sizeof(struct sockaddr));
    if (ret == -1)
    {
        perror("bind");
        exit(-1);
    }
    listen(sock, 5);
	while (1)
	{
		listenfd = accept(sock, NULL, NULL);//从这里开始就要重新监听！！！
		if (listenfd == -1)
		{
			perror("accpet");
			exit(-1);
		}


		//多进程接收
		pid_t pid = 0;
		pid = getpid();
		pid = fork();
		if (pid == 0)
		{
			while (1)
			{
				memset(buf, 0, sizeof(buf));
				//接收
				ret = recv(listenfd, buf, 1024, 0);
				if (ret == -1)
				{
					perror("recv");
					exit(-1);
				}
				if (ret == 0)
				{
					printf("客户端已关闭\n");
					break;
				}
				printf("recv:%s\n", buf);

				//字符串解析成json并回发结果
				struct msg k;
				cJSON* json = NULL;
				json = cJSON_Parse(buf);
				if (json == NULL)
				{
					printf("服务器解析error\n");
					exit(-1);
				}

				cJSON* num1 = NULL;
				num1 = cJSON_GetObjectItem(json, "number1");
				if (num1 == NULL) {
					printf("读num1,error\n");
				}
				if (num1->type == cJSON_Number) {
					k.number1 = num1->valueint;
				}
				cJSON* num2 = NULL;
				num2 = cJSON_GetObjectItem(json, "number2");
				if (num2 == NULL) {
					printf("读num2,error\n");
				}
				if (num2->type == cJSON_Number) {
					k.number2 = num2->valueint;
				}

				cJSON* op = NULL;
				op = cJSON_GetObjectItem(json, "operator");
				if (op == NULL) {
					printf("读运算符,error\n");
				}
				if (op->type == cJSON_String)
				{
					strcpy(k.operator, op->valuestring);
					if (strcmp(k.operator, "add") == 0)
					{
						k.result = k.number1 + k.number2;
						cJSON* json = NULL;
						json = cJSON_CreateObject();
						cJSON_AddNumberToObject(json, "result", k.result);
						char* dest = cJSON_Print(json);
						printf("dest:%s\n", dest);
						ret = send(listenfd, dest, sizeof(struct msg), 0);
						if (ret == -1) {
							perror("sendto");
						}
					}
					else if (strcmp(k.operator, "sub") == 0)
					{
						k.result = k.number1 + k.number2;
						cJSON* json = NULL;
						json = cJSON_CreateObject();
						cJSON_AddNumberToObject(json, "result", k.result);
						char* dest = cJSON_Print(json);
						printf("dest:%s\n", dest);
						ret = send(listenfd, dest, sizeof(struct msg), 0);
						if (ret == -1) {
							perror("sendto");
						}
					}
					else if (strcmp(k.operator, "mul") == 0)
					{
						k.result = k.number1 + k.number2;
						cJSON* json = NULL;
						json = cJSON_CreateObject();
						cJSON_AddNumberToObject(json, "result", k.result);
						char* dest = cJSON_Print(json);
						printf("dest:%s\n", dest);
						ret = send(listenfd, dest, sizeof(struct msg), 0);
						if (ret == -1) {
							perror("sendto");
						}
					}
					else if (strcmp(k.operator, "div") == 0)
					{
						k.result = k.number1 + k.number2;
						cJSON* json = NULL;
						json = cJSON_CreateObject();
						cJSON_AddNumberToObject(json, "result", k.result);
						char* dest = cJSON_Print(json);
						printf("dest:%s\n", dest);
						ret = send(listenfd, dest, sizeof(struct msg), 0);
						if (ret == -1) {
							perror("sendto");
						}
					}
					cJSON_Delete(json);
				}

			}
			printf("子进程退出\n");
			exit(0);
		}


	}
	//等待子进程结束
	wait(NULL);
    // 关闭
    close(listenfd);
    close(sock);

    return 0;

}


